package cn.lf.android.lawfirm.inputsource.conntroller;

import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.inputsource.model.AcceptingDepartment;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

import static cn.lf.android.lawfirm.MyApplication.context;

/**
 * 受理部门controller
 * Created by Administrator on 2017/8/7.
 */

public class AcceptingDepartmentController extends BaseController {

    private AcceptingDepartmentCallback mAcceptingDepartmentCallback;
    private AcceptingDepartmentChangeCallback mAcceptingDepartmentChangeCallback;

    public AcceptingDepartmentController(AcceptingDepartmentCallback InputCallback) {
        this.mAcceptingDepartmentCallback = InputCallback;
    }

    public AcceptingDepartmentController(AcceptingDepartmentCallback InputCallback, AcceptingDepartmentChangeCallback acceptingDepartmentChangeCallback) {
        this.mAcceptingDepartmentCallback = InputCallback;
        this.mAcceptingDepartmentChangeCallback = acceptingDepartmentChangeCallback;
    }

    public void onAcceptingDepartmentChange(String caseID, String levelID, String departmentID) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseID", caseID);
        params.put("levelID", levelID);
        params.put("departmentID", departmentID);
        new MyOkHttpClient().doPost(AppConfig.LsysDept_CaseDepartment, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("response==部门修改=" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mAcceptingDepartmentChangeCallback.onChangeSuccessed("");
                            } else {
                                mAcceptingDepartmentChangeCallback
                                        .onChangeFailed("");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mAcceptingDepartmentChangeCallback.onChangeFailed(errorMsg);
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public void onAcceptingDepartment() {
//        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("Authorization", spUtil.getAccessToken());
        new MyOkHttpClient().doGet(AppConfig.LSYSDEPT, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("response==部门=" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<AcceptingDepartment> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<AcceptingDepartment>>() {
                                                });
                                mAcceptingDepartmentCallback.onSuccessed(beans);
                            } else {
                                mAcceptingDepartmentCallback
                                        .onFailed("");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mAcceptingDepartmentCallback.onFailed(errorMsg);
//                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public void onCaseAcceptingDepartment(String caseID, String levelID) {
//        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("caseID", caseID);
        params.put("levelID", levelID);
        params.put("Authorization", spUtil.getAccessToken());
        DebugLog.e("caseID6=" + caseID);
        DebugLog.e("levelID6=" + levelID);
        new MyOkHttpClient().doGet(AppConfig.LsysDept_AcceptDepartment, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("response==部门=" + response);
                        JSONObject myJsonObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                arraylist = myJsonObject.getJSONArray("responsObject")
                                        .toString();
                                List<AcceptingDepartment> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<AcceptingDepartment>>() {
                                                });
                                mAcceptingDepartmentCallback.onSuccessed(beans);
                            } else {
                                mAcceptingDepartmentCallback
                                        .onFailed("");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("response==部门失败=" + errorMsg);
                        mAcceptingDepartmentCallback.onFailed(errorMsg);
//                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface AcceptingDepartmentCallback {
        public void onSuccessed(List<AcceptingDepartment> beans);

        public void onFailed(String errorMsg);
    }

    public interface AcceptingDepartmentChangeCallback {
        public void onChangeSuccessed(String beans);

        public void onChangeFailed(String errorMsg);
    }
}
